<template>
  <div>
    <h2>子组件</h2>
    <p>父组件传递的name: {{ name }}</p>
    <button @click="handClick">改变name</button>
    <hr />
    <Child2 v-bind="$attrs"></Child2>
  </div>
</template>

<!-- <script>
import Child2 from './Child2.vue'
export default {
  name: 'Child',
  components: { Child2 },
  props: ['name'], // 这里可以接收，也可以不接收
  methods: {
    handClick() {
      this.$emit('changName', '李四')
    }
  },
  // 在Vue 3中，$listeners 已被删除,this.$listeners获取不到
  mounted() {
    // 如果props接收了name 就是 { title:标题 }，否则就是{ name:"张三",  title:标题}
    console.log('儿子组件$attrs:', this.$attrs)
  }
}
</script> -->

<script setup>
import { onMounted, useAttrs } from 'vue'
import Child2 from './Child2.vue'
defineOptions({
  name: 'Child'
})
defineProps(['name'])
const emit = defineEmits(['changName'])
const handClick = () => {
  emit('changName', '李四')
}
const attrs = useAttrs()
onMounted(() => {
  console.log('儿子组件$attrs:', attrs)
})
</script>
